﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UCL.Data;
using UCL.Forms;

namespace UAS5
{
    /// <summary>
    /// 2013-6-7 by sutroon
    /// </summary>
    public partial class frmUrlEdit : Form
    {
        public int RecordID { get; set; }
        public string RecordTags { get; set; }
        AppDataHelper adh = new AppDataHelper();
        public frmUrlEdit()
        {
            InitializeComponent();
        }

        private void frmUrlEdit_Load(object sender, EventArgs e)
        {
            DataTable dtTags = adh.ExecuteDataTable("select Name from tgeneralcatalog where SiteTag='quickdial'");
            lboxTags.DataSource = dtTags;
            lboxTags.DisplayMember = "Name";
            lboxTags.ValueMember = "Name";
            if (!string.IsNullOrEmpty(RecordTags)) lboxTags.SelectedValue = RecordTags;

            LoadData();
        }
        private void LoadData()
        {
            if (RecordID == 0) return;
            DataTable dt = adh.ExecuteDataTable("select * from tquickdial where ID=" + RecordID);
            if (dt.Rows.Count > 0)
            {
                tbName.Text = dt.Rows[0]["Name"].ToString();
                tbPaths.Text = dt.Rows[0]["Paths"].ToString();
                tbRemark.Text = dt.Rows[0]["Remark"].ToString();
                lboxTags.SelectedValue = dt.Rows[0]["Tags"].ToString();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            DataModule dm = new DataModule("tquickdial", adh);
            dm.AddParam("Name", tbName.Text, "名称未填写");
            dm.AddParam("Paths", tbPaths.Text, "路径未填写");
            dm.AddParam("Remark", tbRemark.Text);
            dm.AddParam("Tags", lboxTags.SelectedValue, "标签未填写");
            bool result;
            if (RecordID == 0)
            {
                result = dm.Insert();
            }
            else
            {
                result = dm.Update("ID=" + RecordID);
            }
            MessageBox.Show(result ? "保存成功" : dm.GetError());
            if (result)
            {
                this.RefreshOwner<frmMain>("FormLoad_Icons");
                this.Close();
            }
        }
    }
}
